package org.southyin.securitysamples.controller;

import org.southyin.securitysamples.commons.CommonsResp;
import org.southyin.securitysamples.model.Customer;
import org.southyin.securitysamples.model.Role;
import org.southyin.securitysamples.model.UserBean;
import org.southyin.securitysamples.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping("/")
    public List<UserBean> listUser(String keyWords) {
        List<UserBean> userBeans = userService.listUser(keyWords);
        for (UserBean userBean : userBeans) {
            userBean.setRole(new ArrayList<>());
            List<Role> roles = userBean.getRoles();
            for (Role role : roles) {
                userBean.getRole().add(role.getId());
            }
        }
        return userBeans;
    }

    @PutMapping("/")
    public CommonsResp updateCustomer(@RequestBody UserBean userBean) {
        return userService.updateCustomer(userBean);
    }

    @PutMapping("/updateRole")
    public CommonsResp updateRole(@RequestBody UserBean userBean) {
        return userService.updateRole(userBean);
    }
}
